package com.example.demo2.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.HandlerInterceptor;

import com.example.demo2.common.api.context.LqContext;

public class BaseInterceptor implements HandlerInterceptor {
	private String allowOrigin = "*";
	private String allowMethods = "GET,HEAD,OPTIONS,POST,PUT";
	private String allowHeaders = "Content-Type, Content-Length, token, X-Requested-With";

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
		response.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigin);
		response.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, allowMethods);
		response.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, allowHeaders);
		response.addHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "3600");

		ServletWebRequest webRequest = new ServletWebRequest(request, response);
		String method = request.getMethod();

		LqContext.setContext(webRequest, new LqContext(webRequest, method));

		return true;
	}

}
